﻿@using WebSite.Views
@using ControleRepresentante.Logic.Models
@using WebSite.ViewModels
@using WebSite.ViewModels.IndexViewModels
@using Telerik.Web.Mvc.UI

@model IndexViewModel

<h2>@Model.Title</h2>

@{Html.Telerik().Window()
    .Name("FieldsWindow")
    .Title("Selecione os campos a serem exibidos")
    .Draggable(true)
    .Resizable(x => x.Enabled(false))
    .Modal(true)
    .Visible(false)
    .Content(@<text>
        @using (Html.BeginForm("SetFields", Model.ControllerName, FormMethod.Post, new { id = "setFieldsForm", name = "setFieldsForm" }))
        {
            @Model.GetFieldsFormLayoutViewModel(Html).Render(5)
            <div style="text-align:right">
                @Html.Buttons().Add(ButtonType.Insert, "#", "Alterar Campos", "javascript:setFieldsForm.submit()")
            </div>
        }
        </text>)
    .Render();}

<script language="javascript" type="text/javascript">
    function openFieldWindow()
    {
        $('#FieldsWindow').data('tWindow').open();
    }
</script>

@{
    FormLayoutViewModel searchFormLayoutViewModel = Model.GetSearchFormLayoutViewModel(Html); 

    IHtmlString list = MvcHtmlString.Create(Html.Telerik().Grid<ModelBase>()
        .Footer(false)
        .BindTo(Model.Models)
        .Name(Model.ControllerName)
        .EnableCustomBinding(true)
        .Columns(configurator =>
        {
            Model.IndexColumns(configurator, Html);
            configurator
                .Template(model => Model.GetIndexGridButtonsViewModel(model).Render(Html))
                .Width(78);
        })
        .ToolBar(configurator =>
        {
            configurator.Template(
            @<text>
                @if (Model.CanCreate)
                {
                    <a class="t-button" href="@Url.Action("Create", Model.ControllerName)"><span class='t-add t-icon'></span>&nbsp;Adicionar</a>
                }
                <a href='#' onclick='javascript:openFieldWindow(); ' class='t-button'><img style="vertical-align:middle" src="@Url.Content("~/Content/configuracoes.png")"/>&nbsp;Alterar Colunas</a>
                @if (Model.CanPrint)
                {
                    <a href='@Url.Action("PrintList", Model.ControllerName, Model.GetRouteValues(Request))' target="_blank" class='t-button'><img style="vertical-align:middle" src="@Url.Content("~/Content/print.png")"/>&nbsp;Imprimir</a>
                }
            </text>);
        })
        .DataBinding(dataBindings =>
        {
            if (searchFormLayoutViewModel != null)
            {
                dataBindings.Server().Select("Search", Model.ControllerName, Model.GetSearchRouteValueDictionary());
            }
        })
        .Resizable(configurator => configurator.Columns(true))
        .Pageable(pagerAction => pagerAction
            .Position(GridPagerPosition.Both)
            .Total(Model.ModelCount)
            .PageSize(Model.PageSize)
            .Enabled(true))
        .ToHtmlString());
    TabsViewModel tabsViewModel = Model.GetExtraTabs(Html);
    
    if (searchFormLayoutViewModel != null)
    {
        tabsViewModel.Add("Busca", "~/Content/search.png", new MvcHtmlString(
                "<form action='"+ Url.Action("Search", Model.ControllerName) + "' method='post'>" +
                searchFormLayoutViewModel.Render(2).ToHtmlString() +
                "<div style='text-align:right'><input type='submit' value='Buscar'/></div></form>"));
    }

    tabsViewModel.Insert(0, "Listagem", "~/Content/list.png", list);
}

@tabsViewModel